<!doctype html>
<!--
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<html>
<head>
  <title>test: forceReady</title>
  <script src="../../../webcomponentsjs/webcomponents.js"></script>
  <link rel="import" href="../../polymer.html">
  <script src="../../../tools/test/htmltest.js"></script>
  <script src="../../../tools/test/chai/chai.js"></script>
</head>
<body>
  <polymer-element name="x-foo" noscript>
    <template>x-foo</template>    
  </polymer-element>

  <polymer-element name="x-bar">
    <template>x-bar</template>    
  </polymer-element>

  <polymer-element name="x-baz" extends="x-bar">
    <template>x-baz</template>    
  </polymer-element>


  <x-foo></x-foo>
  <x-bar></x-bar>
  <x-baz></x-baz>
  <x-zot></x-zot>

  <script>
    function testCreated(name) {
      var e = document.querySelector(name);
      chai.assert.isTrue(Polymer.instanceOfType(e, name));
    }

    addEventListener('HTMLImportsLoaded', function() {
      setTimeout(function() {
        // test waitingFor
        var elements = Polymer.waitingFor();
        chai.assert.equal(elements.length, 2);
        chai.assert.equal(elements[0].name, 'x-bar');
        chai.assert.equal(elements[1].name, 'x-baz');
        // test forceReady
        Polymer.forceReady();
      }, 50);
    });
    addEventListener('polymer-ready', function() {
      var elements = Polymer.waitingFor();
      chai.assert.equal(elements.length, 2);
      chai.assert.equal(elements[0].name, 'x-bar');
      chai.assert.equal(elements[1].name, 'x-baz');
      Polymer.whenReady(function() {
        chai.assert.equal(Polymer.waitingFor(), 0);
        testCreated('x-foo');
        testCreated('x-bar');
        testCreated('x-baz');
        //
        // dynamic creation
        var p = document.createElement('polymer-element');
        p.setAttribute('name', 'x-zot');
        p.init();
        var elements = Polymer.waitingFor();
        chai.assert.equal(elements.length, 1);
        chai.assert.equal(elements[0].name, 'x-zot');
        //
        // forceReady delayed
        Polymer.forceReady(50);
        Polymer.whenReady(function() {
          var elements = Polymer.waitingFor();
          chai.assert.equal(elements.length, 1);
          chai.assert.equal(elements[0].name, 'x-zot');
          Polymer('x-zot');
          Polymer.whenReady(function() {
            testCreated('x-zot');
            done();
          });
        });
      });
      // test registering after forceReady
      Polymer('x-bar');
      Polymer('x-baz');
    });
  </script>
</body>
</html>
